package org.cerberus.platform.internal.components;

import java.math.BigDecimal;
import java.util.List;

import org.cerberus.platform.api.AttributeEvent;
import org.cerberus.platform.api.IAttributeListener;
import org.cerberus.platform.api.IPlatformContext;
import org.cerberus.platform.api.PAttribute;
import org.cerberus.platform.api.PComponent;
import org.cerberus.platform.api.PEvent;
import org.cerberus.platform.api.PInject;
import org.cerberus.platform.api.PlatformUtilities;

@PComponent(classID="sales.CurrencyCalculatorView")
public class CurrencyCalculatorView {

	private List<Object> originalCurrencies;
	
	@PAttribute
	private List<CalculatorCurrency> availableCurrencies;
	@PAttribute
	private CalculatorCurrency sourceCurrency;
	@PAttribute
	private CalculatorCurrency targetCurrency;
	@PAttribute
	private BigDecimal sourceAmount;
	@PAttribute
	private BigDecimal targetAmount;
	
	@PInject
	private IPlatformContext context;
	
	public CurrencyCalculatorView(List<Object> currencies) {
		PlatformUtilities.inject(this);
		originalCurrencies = currencies;
		availableCurrencies = convertMasterdata(currencies);
		sourceCurrency = null; // define
		targetCurrency = null; // define
		
		PlatformUtilities.addAttributeListener(this, "sourceAmount", new IAttributeListener() {
			@Override
			public void attributeChanged(AttributeEvent event) {
				calculate();
			}
		});
	}
	
	PAttributeChanged(attributes=new String[]{"sourceAmount", "sourceCurrency", "targetCurrency"})
	private void calculate() {
		
	}
	
	private List<CalculatorCurrency> convertMasterdata(List<Object> masterdata) {
		return null;
	}
	
	
	
	@PEvent
	public void takeSum() {
		setSourceAmount(new BigDecimal(15));
	}
	
	@PEvent
	public void copySum() {
		Object result = PlatformUtilities.eventAndWait(this, "copySum");
	}
	
	@PEvent
	public void pastSourceAmount() {
		PlatformUtilities.event(this, "pastSourceAmount");
	}
	
	public List<CalculatorCurrency> getAvailableCurrencies() {
		return availableCurrencies;
	}

	public CalculatorCurrency getSourceCurrency() {
		return sourceCurrency;
	}

	public void setSourceCurrency(CalculatorCurrency sourceCurrency) {
		this.sourceCurrency = sourceCurrency;
		PlatformUtilities.change(this, "sourceCurrency");
	}


	public CalculatorCurrency getTargetCurrency() {
		return targetCurrency;
	}


	public void setTargetCurrency(CalculatorCurrency targetCurrency) {
		this.targetCurrency = targetCurrency;
		PlatformUtilities.change(this, "targetCurrency");
	}


	public BigDecimal getSourceAmount() {
		return sourceAmount;
	}

	public void setSourceAmount(BigDecimal sourceAmount) {
		this.sourceAmount = sourceAmount;
		PlatformUtilities.change(this, "sourceAmount");
	}

	public BigDecimal getTargetAmount() {
		return targetAmount;
	}


	@PComponent(classID="sales.CurrencyCalculatorView.CalculatorCurrency")
	public static class CalculatorCurrency {
		
		@PAttribute
		private Integer id;
		
		@PAttribute
		private String name;

		public Integer getId() {
			return id;
		}

		public String getName() {
			return name;
		}
	}
	
	public static void show(List<Object> currencies) {
		CurrencyCalculatorView view = new CurrencyCalculatorView(currencies);
		PDialog dialog = view.createModalDialog();
		dialog.bindEvent(OTHER_ACTION1, view, "takeSum");
		dialog.bindEvent(OTHER_ACTION2, view, "copySum");
	}
	
	
}
